using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Audio;

namespace S2Engine.Audio
{

    /// <summary>
    /// Abstracts away the sounds for a simple interface using the Sounds enum
    /// </summary>
    public class SoundManager : IDisposable
    {
        private AudioEngine engine = null;
        private WaveBank wavebank = null;
        private SoundBank soundbank = null;
        private bool disposed = false;

        public SoundManager() { }
        
        ~SoundManager()
        {     
            Dispose(false);
        } 

        /// <summary>
        /// Plays a sound
        /// </summary>
        /// <param name="sound">Which sound to play</param>
        /// <returns>XACT cue to be used if you want to stop this particular looped sound. Can be ignored for one shot sounds</returns>
        public Cue Play(string soundName)
        {
            Cue returnCue = soundbank.GetCue(soundName);
            returnCue.Play();
            return returnCue;
        }

        

        /// <summary>
        /// Stops a previously playing cue
        /// </summary>
        /// <param name="cue">The cue to stop that you got returned from Play(sound)</param>
        public void Stop(Cue cue)
        {
            cue.Stop(AudioStopOptions.Immediate);
        }

        /// <summary>
        /// Starts up the sound code
        /// </summary>
        public void Initialize(string xgsFileName, string xwbFileName, string xsbFileName)
        {
            engine = new AudioEngine(xgsFileName);
            wavebank = new WaveBank(engine, xwbFileName);
            soundbank = new SoundBank(engine, xsbFileName);
        }

        public void Update()
        {
            engine.Update();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    soundbank.Dispose();
                    wavebank.Dispose();
                    engine.Dispose();
                }
                disposed = true;
            }
        }     
    }
}
